﻿using AutoMapper;
using Modules.Attachment;
using Modules.Corp;
using Modules.Manager;
using Modules.Project;
using Modules.System.Area;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Models.Project
{
    /// <summary>
    /// 项目基础信息Dto
    /// </summary>
    public class ProjectBaseInfo : ProjectInfo
    {
        /// <summary>
        /// 行业主管部门
        ///</summary>
        public string? IndustryTypeName { get; set; }

        /// <summary>
        /// 项目分类。
        ///</summary>
        public string? CategoryName { get; set; }

        /// <summary>
        /// 项目状态。
        ///</summary>
        public string? PrjStatusName { get; set; }

        /// <summary>
        /// 立项级别。
        ///</summary>
        public string? ApprovalLevelName { get; set; }

        /// <summary>
        /// 建设规模。
        ///</summary>
        public string? SizeTypeName { get; set; }

        /// <summary>
        /// 建设性质。
        ///</summary>
        public string? PropertyNumName { get; set; }

        /// <summary>
        /// 工程用途。
        ///</summary>
        public string? FunctionNumName { get; set; }

        /// <summary>
        /// 项目种类
        /// </summary>
        public string? FundsSourceName { get; set; }

        /// <summary>
        /// 审核状态
        ///</summary>
        public string? AuditStatusName { get; set; }

        /// <summary>
        /// 数据来源
        /// </summary>
        public string? DataSourceName { get; set; }

        /// <summary>
        /// 中标通知书
        /// </summary>
        public List<AttachmentsForm<int>> LetterAttachments { get; set; } = new List<AttachmentsForm<int>>();

        /// <summary>
        /// 担保文件
        /// </summary>
        public List<AttachmentsForm<int>> EarnestAttachments { get; set; } = new List<AttachmentsForm<int>>();

        /// <summary>
        /// 创建管理员
        /// </summary>
        public string? CreateManagerName { get; set; }

        /// <summary>
        /// 总承包企业名称
        /// </summary>
        public string? LumpContractorCorpName { get; set; }

        /// <summary>
        /// 总承包企业统一信用代码
        /// </summary>
        public string? LumpContractorCorpCode { get; set; }

        public string? CityName { get; set; }
    }

    /// <summary>
    /// 基础信息扩展属性
    /// </summary>
    public static class ProjectBaseInfoExceptions
    {
        /// <summary>
        /// </summary>
        /// <param name="projectInfo"></param>
        /// <returns></returns>
        public static ProjectBaseInfo AsBaseInfo(this ProjectInfo projectInfo, IApplicationContext applicationContext)
        {
            ProjectBaseInfo baseInfo = new();
            var dto = ServiceAccessor.GetService<IMapper>().Map(projectInfo, baseInfo);
            dto.CategoryName = projectInfo.Category?.GetDescription();
            dto.AuditStatusName = projectInfo.AuditStatus?.GetDescription();
            dto.IndustryTypeName = projectInfo.IndustryType.GetDescription();
            dto.PrjStatusName = projectInfo.PrjStatus?.GetDescription();
            dto.SizeTypeName = projectInfo.SizeType?.GetDescription();
            dto.PropertyNumName = projectInfo.PropertyNum?.GetDescription();
            dto.FundsSourceName = projectInfo.FundsSource?.GetDescription();
            dto.ApprovalLevelName = projectInfo.ApprovalLevel?.GetDescription();
            dto.FunctionNumName = projectInfo.FunctionNum?.GetDescription();
            dto.DataSourceName = projectInfo.DataSource?.GetDescription();

            var areaService = applicationContext.GetService<AreaService>();
            dto.CityName = areaService.GetAreaFullName(540000, projectInfo.CityCode, projectInfo.AreaCode);
            var managerService = ServiceAccessor.GetService<ManagerService>();
            var manager = managerService.GetManagerInfo(dto.CreateManagerId ?? 0);
            if (manager != null)
                dto.CreateManagerName = manager.Name;
            var corpService = ServiceAccessor.GetService<CorpService>();
            var corp = corpService.GetCorpInfo(dto.LumpContractorCorpId);
            if (corp != null)
            {
                dto.LumpContractorCorpName = corp.Name;
                dto.LumpContractorCorpCode = corp.Code;
            }

            var projectAttachmentService = ServiceAccessor.GetService<ProjectAttachmentService>();
            var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(projectInfo.ProjectId, 附件类型.中标通知书, 项目附件租户.Project);
            foreach (var item in attachments)
            {
                AttachmentsForm<int> attachmentsForm = new()
                {
                    AttachmentId = item.Id,
                    AttachmentName = item.FriendlyName,
                };
                dto.LetterAttachments.Add(attachmentsForm);
            }

            var attachments2 = projectAttachmentService.GetSysAttachmentByObjectIdAndType(projectInfo.ProjectId, 附件类型.担保文件, 项目附件租户.Project);
            foreach (var item in attachments2)
            {
                AttachmentsForm<int> attachmentsForm = new()
                {
                    AttachmentId = item.Id,
                    AttachmentName = item.FriendlyName,
                };
                dto.EarnestAttachments.Add(attachmentsForm);
            }
            return dto;
        }
    }
}